<template>
  <div class="page-container">
    <div class="control-container">
      <div class="right-panel">
        <div class="search-panel">
          <el-form :inline="true" label-width="100">
            <el-form-item label="行业类别">
              <el-select v-model="query.industryCategory" clearable placeholder="请选择">
                <el-option
                  v-for="item in industryCategorys"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="控制级别">
              <el-select v-model="query.controlLevel" clearable placeholder="请选择">
                <el-option
                  v-for="item in controlLevels"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="企业名称">
              <el-input v-model="query.queryField" />
            </el-form-item>
            <el-button type="primary" @click="handleSelect">查询</el-button>
            <el-button v-if="hasBtn('enterprise:export')" :loading="saveLoading" type="primary" @click="handleExport">导出</el-button>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 300px)"
            :data="tableData"
          >
            <el-table-column align="center" type="index" width="80" />
            <el-table-column prop="name" label="企业名称" />
            <el-table-column prop="controlLevel" label="控制级别">
              <template slot-scope="scope">{{ getDictLabel('e014', scope.row.controlLevel) }}</template>
            </el-table-column>
            <el-table-column prop="organizationCode" label="组织机构代码" />
            <el-table-column prop="legalRepresentative" label="法人代表" />
            <el-table-column prop="enterpriseAddressDetails" label="企业地址" />
            <el-table-column prop="zipCode" label="邮编" />
            <el-table-column prop="fax" label="传真" />
            <el-table-column prop="contacts" label="联系人" />
            <el-table-column prop="contactInformation" label="联系方式" />
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="handle('detail',scope.row)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddDialog
      :dialog-visible="dialogVisible"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import { getDict } from '@/utils/dict'
import { getDictLabel } from '@/utils'
import Pagination from '@/components/Pagination'
import { getPage } from '@/api/base/enterprise'
import { hasBtnPermission as hasBtn } from '@/utils/buttons'
import AddDialog from '@/views/system/customer/components/AddDialog'
import { download } from '@/api/base/enterprise'
import { downloadFile } from '@/utils'

export default {
  name: 'Enterprise',
  components: {
    Pagination, AddDialog
  },
  data() {
    return {
      saveLoading: false,
      query: {
        total: 0,
        page: 1,
        limit: 10,
        controlLevels: '',
        industryCategorys: '',
        queryField: ''
      },
      tableData: [],
      tableLoading: false,
      controlLevels: [],
      industryCategorys: [],
      dialogVisible: false,
      dialogType: 'detail',
      obj: {}
    }
  },
  created() {
    this.controlLevels = getDict('e014')
    this.industryCategorys = getDict('e013')
    this.initData()
  },
  methods: {
    getDictLabel, hasBtn,
    initData() {
      this.handleSelect()
    },
    // 查询
    handleSelect() {
      this.tableLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      getPage(query).then(res => {
        this.tableData = res.data.data.list
        this.query.total = res.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 导出
    handleExport() {
      this.saveLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      download(query).then(result => {
        downloadFile(result, '企业档案', 'xlsx')
        this.saveLoading = false
      }).catch(() => {
        this.saveLoading = false
      })
    },
    // 修改
    handle(dialogType, row) {
      this.obj = row
      this.dialogType = dialogType
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
